package game.zqll.systems;

import com.artemis.BaseSystem;
import com.artemis.Entity;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import game.zqll.components.*;

import static game.zqll.ModSampleApplication.worldHeight;
import static game.zqll.ModSampleApplication.worldWidth;

public class SpawnSystem extends BaseSystem {

    protected float acc;
    /** How long to wait between updates. */
    private final float interval = 1f;

    private float intervalDelta;

    @Override
    protected void initialize() {
        processSystem();
        processSystem();
        processSystem();
    }

    @Override
    protected boolean checkProcessing() {
        acc += world.delta;
        if(acc >= interval) {
            acc -= interval;
            intervalDelta = (acc - intervalDelta);
            acc += MathUtils.random(0, interval * 0.8f);
            return true;
        }
        return false;
    }


    @Override
    protected void processSystem() {
        Entity spawnEntity = world.createEntity();
        SpawnerComp spawnerComp = spawnEntity.edit().create(SpawnerComp.class);
        spawnerComp.spawnId = "drop";

        int size = 32;
        PosComp posComp = spawnEntity.edit().create(PosComp.class);
        posComp.x = MathUtils.random(0, worldWidth - size);
        posComp.y = worldHeight;

        MoveComp moveComp = spawnEntity.edit().create(MoveComp.class);
        moveComp.velocity = new Vector2(0, MathUtils.random(-100f, -170f));

        BorderComp borderComp = spawnEntity.edit().create(BorderComp.class);
        SizeComp sizeComp = spawnEntity.edit().create(SizeComp.class);
        sizeComp.width = size;
        sizeComp.height = size;
    }
}
